// Package alertconverter provides functions to convert alerts from different sources
// to the unified AlertData format used by the system.
package alertconverter

import (
	"net/url"
	"time"
)

// getStringFromMap 从map中安全获取字符串值
func getStringFromMap(m map[string]string, key, defaultValue string) string {
	if val, ok := m[key]; ok {
		return val
	}
	return defaultValue
}

// getSeverityText 获取严重级别文本
func getSeverityText(severity int) string {
	switch severity {
	case 1:
		return "紧急"
	case 2:
		return "重要"
	case 3:
		return "警告"
	case 4:
		return "信息"
	default:
		return "未知"
	}
}

// getCardColorBySeverity 根据严重级别获取卡片颜色
func getCardColorBySeverity(severity int) string {
	switch severity {
	case 1:
		return "red"
	case 2:
		return "orange"
	case 3:
		return "yellow"
	case 4:
		return "blue"
	default:
		return "blue"
	}
}

// ParseLabelsFromURL 从URL中解析标签
func ParseLabelsFromURL(rawURL string) []string {
	labels := []string{}

	// 解析URL
	parsedURL, err := url.Parse(rawURL)
	if err != nil {
		return labels
	}

	// 解析查询参数
	queryParams := parsedURL.Query()

	// 获取label参数
	if labelParams := queryParams["label"]; len(labelParams) > 0 {
		labels = append(labels, labelParams...)
	}

	return labels
}

// hashStringToInt creates a simple hash of a string to use as an ID
func hashStringToInt(s string) int {
	hash := int64(0)
	for _, c := range s {
		hash = 31*hash + int64(c)
	}
	// 确保ID为正数
	if hash < 0 {
		hash = -hash
	}
	// 限制ID长度以避免过大
	return int(hash % 1000000)
}

// getCurrentTimestamp 获取当前时间戳
func getCurrentTimestamp() int64 {
	return time.Now().Unix()
}

// getCurrentFormattedTime 获取当前格式化时间
func getCurrentFormattedTime() string {
	return time.Now().Format("2006-01-02 15:04:05")
}
